//
//  BaseViewController.m
//  MUBATE
//
//  Created by sunbin on 2017/2/14.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "BaseViewController.h"
#import "logOut.h"
#import "Login.h"
// 格式 0xff3737
#define JHUDRGBHexAlpha(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]

#define JHUDRGBA(r,g,b,a)     [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController.navigationBar lt_reset];

    }else{
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
        [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    }
    
    // 网络状态变化
    [ZL_NetworkHelper startListeningNetwork];


}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor themeColor];
    
    // 检查网络
    [self checkNetworkStatus];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(logOutEven) name:LOGOUT object:nil];

}

- (void)checkNetworkStatus{
    WEAKSELF;
    [ZL_NetworkHelper checkNetworkStatusWithBlock:^(ZL_NetworkStatus status) {
        if (status == ZL_NetworkStatusNotReachable) {
            //显示无网络时 需要关闭jhud加载
            if (_isNoShowNoNetError) {
            }else{
                [weakSelf.navigationController.view showError:@"网络故障"];
            }
            
            
        }else{
            [[NSNotificationCenter defaultCenter] postNotificationName:NETWORKSTATUS object:weakSelf];
        }
    }];
}
// 退出操作
- (void)logOutEven{
    NSString *account = [[logOut sharedlogOut] logOutOperation];

    
    [self jumpLog:account];

}


//退出登录
- (void)jumpLog:(NSString *)logAccount{

    WEAKSELF;
    Login *loginController = [[Login alloc] init];
    loginController.account = logAccount;

    [kAppDelegate.window addSubview:loginController.view];
    [self.view removeFromSuperview];
    kAppDelegate.window.rootViewController = loginController;

    [self presentViewController:loginController animated:NO completion:^{

        [weakSelf.view showSuccess:@"注销成功"];
        
    }];

}











- (void)setJHUDRect:(CGRect)jHUDRect{
    _jHUDRect = jHUDRect;
    // 建议基类中Lazy创建，进行二次封装，使用时直接调用，避免子类中频繁创建产生冗余代码的问题。
    self.hudView = [[JHUD alloc]initWithFrame:jHUDRect];

}


-(void)jhudHide:(UIView *)ctl
{
    WEAKSELF;
    [weakSelf.hudView hide];
}
// 加载 多小点串成圆旋转
-(void)loadingCircleAnimation:(NSString *)messageLabel showAtView:(UIView *)view
{
    WEAKSELF;
    weakSelf.hudView.messageLabel.text = messageLabel;
    weakSelf.hudView.indicatorBackGroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.1];
    weakSelf.hudView.indicatorForegroundColor = [UIColor lightGrayColor];
    [weakSelf.hudView showAtView:view hudType:JHUDLoadingTypeCircle];
}

// 加载 圆旋转

-(void)loadingCircleJoinAnimation:(NSString *)messageLabel showAtView:(UIView *)view
{
    WEAKSELF;

    weakSelf.hudView.messageLabel.text = messageLabel;
    weakSelf.hudView.indicatorForegroundColor = JHUDRGBA(60, 139, 246, .5);
    weakSelf.hudView.indicatorBackGroundColor = JHUDRGBA(185, 186, 200, 0.3);
    [weakSelf.hudView showAtView:view hudType:JHUDLoadingTypeCircleJoin];
}

// 水平点过度
-(void)loadingDotAnimation:(NSString *)messageLabel showAtView:(UIView *)view
{
    WEAKSELF;

    weakSelf.hudView.messageLabel.text = messageLabel;
    weakSelf.hudView.indicatorBackGroundColor = [UIColor whiteColor];
    weakSelf.hudView.indicatorForegroundColor = [UIColor orangeColor];
    [weakSelf.hudView showAtView:view hudType:JHUDLoadingTypeDot];
}


-(void)loadingFailure:(NSString *)messageLabel refreshTitle:(NSString *)refreshTitle customImage:(NSString *)customImage indicatorViewSize:(CGSize)size showAtView:(UIView *)view
{
    WEAKSELF;

    weakSelf.hudView.indicatorViewSize = size;
    weakSelf.hudView.messageLabel.text = messageLabel;
    [weakSelf.hudView.refreshButton setTitle:refreshTitle forState:UIControlStateNormal];
    weakSelf.hudView.customImage = [UIImage imageNamed:customImage];
    
    if ([NHUtils isBlankString:refreshTitle]) {
            weakSelf.hudView.refreshButton.layer.borderWidth = 0;

    }
    

    [weakSelf.hudView showAtView:view hudType:JHUDLoadingTypeFailure];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}




@end
